package io.renren.modules.com.service.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoleilu.hutool.util.CollectionUtil;

import io.renren.common.utils.DateUtils;
import io.renren.common.utils.R;
import io.renren.modules.com.dao.ComPaymentHistoryDao;
import io.renren.modules.com.entity.ComPaymentHistoryEntity;
import io.renren.modules.com.service.ComPaymentHistoryService;

@Service("comPaymentHistoryService")
public class ComPaymentHistoryServiceImpl implements ComPaymentHistoryService {

	@Autowired
	private ComPaymentHistoryDao comPaymentHistoryDao;

	@Override
	public ComPaymentHistoryEntity queryObject(Long serialNo) {
		return comPaymentHistoryDao.queryObject(serialNo);
	}

	@Override
	public List<ComPaymentHistoryEntity> queryList(Map<String, Object> map) {
		List<ComPaymentHistoryEntity> comPaymentHistoryList = comPaymentHistoryDao.queryList(map);
		for (ComPaymentHistoryEntity comPaymentHistoryEntity : comPaymentHistoryList) {
			comPaymentHistoryEntity.setBillingCycle(
					comPaymentHistoryEntity.getChargingSd() + "至" + comPaymentHistoryEntity.getChargingEd());
		}
		return comPaymentHistoryList;
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return comPaymentHistoryDao.queryTotal(map);
	}

	@Override
	public R save(ComPaymentHistoryEntity comPaymentHistory) {
		return savePrivate(comPaymentHistory);
	}

	@Transactional
	private R savePrivate(ComPaymentHistoryEntity comPaymentHistory) {
		ComPaymentHistoryEntity yuiHatano = comPaymentHistoryDao.queryByBillNo(comPaymentHistory.getBillNo());
		if (null != yuiHatano) {
			return R.error("已存在该单据号记录！");
		}
		int month = DateUtils.dateBetween(comPaymentHistory.getChargingSd(), comPaymentHistory.getChargingEd());
		comPaymentHistory.setMonth(month);
		comPaymentHistory.setAmountPayable(comPaymentHistory.getUnivalent() * comPaymentHistory.getAcreage() * month);
		comPaymentHistory.setFund("返租租金");
		comPaymentHistoryDao.save(comPaymentHistory);
		return R.ok();
	}

	@Override
	public R update(ComPaymentHistoryEntity comPaymentHistory) {
		return updatePrivate(comPaymentHistory);
	}

	private R updatePrivate(ComPaymentHistoryEntity comPaymentHistory) {
		ComPaymentHistoryEntity yuiHatano = comPaymentHistoryDao.queryByBillNo(comPaymentHistory.getBillNo());
		if (null == yuiHatano) {
			return R.error("不存在该单据号记录！");
		}
		int month = DateUtils.dateBetween(comPaymentHistory.getChargingSd(), comPaymentHistory.getChargingEd());
		comPaymentHistory.setMonth(month);
		comPaymentHistory.setAmountPayable(comPaymentHistory.getUnivalent() * comPaymentHistory.getAcreage() * month);
		comPaymentHistoryDao.update(comPaymentHistory);
		return R.ok();
	}

	@Override
	public void delete(Long serialNo) {
		comPaymentHistoryDao.delete(serialNo);
	}

	@Override
	public void deleteBatch(Long[] serialNos) {
		comPaymentHistoryDao.deleteBatch(serialNos);
	}

	public void export(OutputStream out, Map<String, Object> map) {
		exportPrivate(out, map);
	}

	private void exportPrivate(OutputStream out, Map<String, Object> map) {
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = workbook.createSheet();
		// 生成数字样式
		HSSFCellStyle numberStyle = workbook.createCellStyle();
		numberStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
		// 生成文字样式
		HSSFCellStyle style = workbook.createCellStyle();
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 生成一个字体
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		style.setFont(font);
		// 生成excel内容
		List<String> colNames = CollectionUtil.newArrayList("序号", "单据号", "合同号", "房号", "业主姓名", "付款时间", "付款方式", "款项",
				"金额", "计费周期", "备注");
		HSSFRow rowOne = sheet.createRow(0);
		HSSFCell cell = null;
		for (int i = 0; i < colNames.size(); i++) {
			cell = rowOne.createCell(i);
			cell.setCellStyle(style);
			cell.setCellValue(colNames.get(i));
		}
		List<ComPaymentHistoryEntity> chinaJoy = queryList(map);
		for (int i = 0; i < chinaJoy.size(); i++) {
			HSSFRow row = sheet.createRow(i + 1);
			cell = row.createCell(0);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getSerialNo());
			cell = row.createCell(1);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getBillNo());
			cell = row.createCell(2);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getContractNo() != null ? chinaJoy.get(i).getContractNo() : "");
			cell = row.createCell(3);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getAssetIds() != null ? chinaJoy.get(i).getAssetIds() : "");
			cell = row.createCell(4);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getProprietor());
			cell = row.createCell(5);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getDue());
			cell = row.createCell(6);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getPaymentMethod());
			cell = row.createCell(7);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getFund() != null ? chinaJoy.get(i).getFund() : "");
			cell = row.createCell(8);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getPayAmount() != null ? chinaJoy.get(i).getPayAmount().toString() : "");
			cell = row.createCell(9);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getBillingCycle() != null ? chinaJoy.get(i).getBillingCycle() : "");
			cell = row.createCell(10);
			cell.setCellStyle(style);
			cell.setCellValue(chinaJoy.get(i).getRemark() != null ? chinaJoy.get(i).getRemark() : "");
		}
		try {
			workbook.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
